/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.Iterator;
import java.util.List;
import logging.CommonsLogging;
import org.apache.log4j.Logger;
import persistence.rb_purchase_persist;
import persistence.rb_raw_mat_stock_persist;
import vo.rb_daily_expences;
import vo.rb_purchase;

/**
 *
 * @author Shuhail
 */
public class PurchaseController {
    Logger logger = CommonsLogging.getInstance().getLogger();
    public static PurchaseController getInstance() {
        
        return new PurchaseController();
    }
    
    
    public boolean insertPurchase(List<rb_purchase> pur) {
        if(logger.isDebugEnabled())
            logger.info("Called InsertPurchase COntroller with "+pur.size()+" items");
        boolean success = false;
        /*
        rb_purchase pur1 = new rb_purchase();
        pur1.setPayment_mode("Cash");
        pur1.setUnit_cost(2);
        //pur1.setDate(date);
        pur1.setAmt_paid(8);
        pur1.setQuantity(6);
        pur1.setVendor_id("V1");
        pur1.setRaw_mat_id("R1");
        pur1.setRemarks("test");
        pur1.setTotal_amt(12);
        
        pur.add(pur1);
        */
        
        Iterator it =pur.iterator();
        while(it.hasNext()) {
            rb_purchase purchase = (rb_purchase) it.next();
            if(logger.isDebugEnabled())
                logger.info("Inserting to purchase table");
            boolean succ=rb_purchase_persist.getInstance().insertIntoPurchase(purchase);
            if(logger.isDebugEnabled())
                logger.info("Updating Raw stock materials");
            succ = rb_raw_mat_stock_persist.getInstance().update(purchase);
            if(!succ)
                success=true;
            rb_daily_expences expence = new rb_daily_expences();
            expence.setPaid_to(purchase.getVendor_id());
            expence.setAmount(purchase.getAmt_paid());
            expence.setRemarks("Purchase of Raw Material - Total Amt:"+purchase.getTotal_amt()+" Amt Paid:"+purchase.getAmt_paid());
        }
        return success;
    }
    
    public boolean updateRawStock(rb_purchase purchase){
        
        
        return true;
    }
    
}
